"""
Case Type   : cm
Case Name   : cm_ctl query 异常参数搭配验证
Create At   : 2024-07
Owner       : haomeng
Description :
    1、查看集群状态
    2、cm_ctl query 异常参数验证
Expect      :
    1、集群所有节点状态正常，显示Normal
    2、合理报错
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class CmCtl(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常,DN主在节点1----'
        self.log.info(text)

        status = self.comsh.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '----step2:cm_ctl query 异常参数验证; expect:合理报错----'
        self.log.info(text)

        param_list = ['-c -s', '-v -s', '-s', '-c -i', '-v -s', '-v -i', '-i',
                      '-c -d', '-v -d', '-d', '-c -x', '-v -x', '-x', '-c -p',
                      '-v -p', '-p']
        for param in param_list:
            res = self.comsh.exec_cm_ctl('query', f'{param}')
            self.log.info(res)
            self.assertTrue('The cmdline and options entered by the user is incorrect' in res
                            or 'The cmdline entered by the user is incorrect' in res, 'query执行结果查询成功')

    def tearDown(self):
        self.log.info('-----No need to clean-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
